package com.example.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.log.InitLog;
import com.example.mapper.ReservationMapper;
import com.example.pojo.BusNews;
import com.example.pojo.PageBean;
import com.example.pojo.Reservation;
import com.example.pojo.Result;
import com.example.service.impl.ReservationServerImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ReservationService extends ServiceImpl<ReservationMapper, Reservation> implements ReservationServerImpl {
    @Autowired
    private ReservationMapper reservationMapper;
    @Override
    @InitLog
    public Result addReservation(Reservation reservation) {
        boolean isSave = save(reservation);
        return isSave?Result.succeed():Result.error("添加失败~");
    }

    @Override
    public Result pageList(PageBean pageBean, String name) {
        int currentPage = pageBean.getPage();
        int pageSize = pageBean.getPageSize();
        //关键词模糊匹配,逻辑删除
        LambdaQueryWrapper<Reservation> wrapper=new LambdaQueryWrapper<Reservation>()
                .like(name!=null&&!name.isEmpty(),Reservation::getActivity,name)
                .eq(Reservation::getIsDeleted,0);
        Page<Reservation> page=new Page<>(currentPage,pageSize);
        page(page,wrapper);
        List<Reservation> reservation = page.getRecords();
        return Result.succeed(reservation);
    }

    @Override
    public Result deleteByIds(List<Integer> ids) {
        update(new LambdaUpdateWrapper<Reservation>()
                .in(Reservation::getId,ids)
                .set(Reservation::getUpdateTime, LocalDateTime.now())
                .set(Reservation::getIsDeleted,1));
        return Result.succeed();
    }

    @Override
    public Result updateReservation(Reservation reservation) {
        //update().set("").eq("id",reservation.getId()).eq("is_deleted",0);
        saveOrUpdate(reservation,new LambdaQueryWrapper<Reservation>()
                .eq(Reservation::getId,reservation.getId())
        );
        return Result.succeed();
    }
}
